﻿using Orchard.ContentManagement.MetaData;
using Orchard.Data.Migration;
using Dyn.News.Models;

namespace Dyn.News {
    public class Migrations : DataMigrationImpl {

        public int Create() {
            SchemaBuilder.CreateTable("CategoryPartArchiveRecord",
                table => table
                    .Column<int>("Id", column => column.PrimaryKey().Identity())
                    .Column<int>("Year")
                    .Column<int>("Month")
                    .Column<int>("PostCount")
                    .Column<int>("CategoryPart_id")
                );

            SchemaBuilder.CreateTable("NewsCategoryPartRecord",
                table => table
                    .ContentPartRecord()
                    .Column<string>("Description", c => c.Unlimited())
                    .Column<int>("ParentID")
                    .Column<int>("OrderDisplay")
                    .Column<int>("PostCount")
                );

            SchemaBuilder.CreateTable("RecentArticlePartRecord",
                table => table
                    .ContentPartRecord()
                    .Column<string>("Articlelug")
                    .Column<int>("Count")
                );

            SchemaBuilder.CreateTable("CategoryArchivesPartRecord",
                table => table
                    .ContentPartRecord()
                    .Column<string>("Categorylug", c => c.WithLength(255))
                );
            SchemaBuilder.CreateTable("ArticlePartRecord",
                table=>table
                    .ContentPartRecord()
                    .Column<string>("Sumary",c=>c.WithLength(1024))
                    .Column<string>("Author",c=>c.WithLength(64))
                );
            ContentDefinitionManager.AlterTypeDefinition(typeof(NewsCategoryPart).Name,
                cfg => cfg
                    .WithPart("NewsCategoryPart")
                    .WithPart("CommonPart")
                    .WithPart("RoutePart")
                    .WithPart("MenuPart")
                    .WithPart("AdminMenuPart", p => p.WithSetting("AdminMenuPartTypeSettings.DefaultPosition", "2"))
                );

            ContentDefinitionManager.AlterTypeDefinition("Article",
                cfg => cfg
                    .WithPart("ArticlePart")
                    .WithPart("MediaListPart")
                    .WithPart("PageViewPart")
                    .WithPart("CommentPart")
                    .WithPart("Tag")
                    .WithPart("CommonPart", p => p
                        .WithSetting("DateEditorSettings.ShowDateEditor", "true"))
                    .WithPart("PublishLaterPart")
                    .WithPart("RoutePart")
                    .WithPart("BodyPart")
                    );
            
            ContentDefinitionManager.AlterTypeDefinition("RecentArticles",
                cfg => cfg
                    .WithPart("RecentArticlesPart")
                    .WithPart("CommonPart")
                    .WithPart("WidgetPart")
                    .WithSetting("Stereotype", "Widget")
                );

            ContentDefinitionManager.AlterTypeDefinition("CategoryArchives",
                cfg => cfg
                    .WithPart("CategoryArchivesPart")
                    .WithPart("CommonPart")
                    .WithPart("WidgetPart")
                    .WithSetting("Stereotype", "Widget")
                );

            return 4;
        }

        public int UpdateFrom1() {
            ContentDefinitionManager.AlterTypeDefinition(typeof(NewsCategoryPart).Name, cfg => cfg.WithPart("AdminMenuPart", p => p.WithSetting("AdminMenuPartTypeSettings.DefaultPosition", "2")));
            return 3;
        }

        public int UpdateFrom2() {
            ContentDefinitionManager.AlterTypeDefinition(typeof(NewsCategoryPart).Name, cfg => cfg.WithPart("AdminMenuPart", p => p.WithSetting("AdminMenuPartTypeSettings.DefaultPosition", "2")));
            return 3;
        }

        public int UpdateFrom3() {
            ContentDefinitionManager.AlterTypeDefinition("Article", cfg => cfg.WithPart("CommonPart", p => p.WithSetting("DateEditorSettings.ShowDateEditor", "true")));
            return 4;
        }

        public int UpdateFrom4()
        {
            SchemaBuilder.CreateTable("NewsCategoryWidgetPartRecord", table => table
                .ContentPartRecord()
                .Column<int>("ViewType")
            );
            SchemaBuilder.CreateTable("NewsContentCategoryWidgetRecord", table => table
                .Column<int>("Id", column => column.PrimaryKey().Identity())
                .Column<int>("NewsCategoryPartRecord_id")
                .Column<int>("NewsCategoryWidgetPartRecord_id")
                .Column<int>("DisplayOrder")
                .Column<bool>("IsEnable")
            );

           
            SchemaBuilder.CreateTable("NewsCategoryMenuWidgetPartRecord", table => table
                .ContentPartRecord());

            SchemaBuilder.CreateTable("NewsContentCategoryMenuWidgetRecord", table => table
                .Column<int>("Id", column => column.PrimaryKey().Identity())
                .Column<int>("Parent")
                .Column<int>("NewsCategoryPartRecord_id")
                .Column<int>("NewsCategoryMenuWidgetPartRecord_id")
                .Column<int>("DisplayOrder")
                .Column<bool>("IsEnable")
            );
            ContentDefinitionManager.AlterTypeDefinition("NewsCategoryMenuProductWidget", cfg => cfg
                .WithPart("NewsCategoryMenuWidgetPart")
                .WithPart("WidgetPart")
                .WithPart("CommonPart")
                .WithSetting("Stereotype", "Widget"));
            return 5;
        }
    }
}